<div class="row animate fadeIn">
    <form id="edit_invoice_form" class="form-horizontal"  method="post">
      <?php foreach($invoice_data as $inv) { ?>
      <input type="hidden" name="hidden_invEdit_id" id="e_invoice_id" value="<?php echo $inv->invoice_id; ?>">
        <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Edit Invoice</i></h5>
            <div class="ibox-tools">    
                <button  id="sads" class="btn btn-sm btn-primary save-btn-tab">Save</button>
                <a id = "cancel_edit" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a>
            </div>
        </div>
        <div class="ibox-content">   
          <div class="form-group">
              <label class="col-lg-2 control-label">Account No</label>
            <div class="col-lg-5">
              <input type="text" id="" name="" class="form-control" value="<?php echo $inv->account_num; ?>" readonly>
            </div>
              <label class="col-lg-2 control-label">Invoice No</label>
            <div class="col-lg-3">
              <input type="text" name="" class="form-control" value="<?php echo $inv->invoice_num; ?>" readonly>
            </div>
          </div>
          <div class="form-group">
              <label class="col-lg-2 control-label">Account Type</label>
            <div class="col-lg-5">
              <input type="text" id="" name="" class="form-control" value="<?php echo $inv->meaning; ?>" readonly>
            </div>
              <label class="col-lg-2 control-label">Invoice Date</label>
            <div class="col-lg-3">
              <input type="text" name="" class="form-control" value="<?php echo date('d-M-Y',strtotime($inv->invoice_date)); ?>" readonly>
            </div>
          </div>
          <div class="form-group">
              <label class="col-lg-2 control-label">Customer Name</label>
            <div class="col-lg-5">
              <input type="text" id="" name="" class="form-control" value="<?php echo $inv->acct_name; ?>" readonly>
            </div>
              <label class="col-lg-2 control-label">GL Date</label>
            <div class="col-lg-3">
              <input type="text" name="" class="form-control" value="<?php echo date('d-M-Y',strtotime($inv->gl_date)); ?>" readonly>
            </div>
          </div>
          <div class="form-group">
              <label class="col-lg-2 control-label">Billing Address</label>
            <div class="col-lg-5">
              <input type="text" id="" name="" class="form-control" value="<?php echo $inv->address_1; ?>" readonly></input>
            </div>
              <label class="col-lg-2 control-label">Due Date</label>
            <div class="col-lg-3">
              <input type="search" name="" id="" class="form-control" value="<?php echo date('d-M-Y',strtotime($inv->due_date)); ?>" readonly>
            </div>
          </div>
          <div class="form-group">
              <label class="col-lg-2 control-label">Charge List</label>
            <div class="col-lg-5">
              <input type="text" id="" name="" class="form-control" value="<?php echo $inv->list_name; ?>" readonly>
            </div>
              <label class="col-lg-2 control-label">Previous Balance</label>
            <div class="col-lg-3">
              <input type="text" name="" class="form-control" value="<?php echo $inv->prev_bill_bal; ?>" readonly>
            </div>
          </div>
          <div class="form-group">
              <label class="col-lg-2 control-label">Receivable Acct</label>
            <div class="col-lg-5">
              <select class="chosen-select" name="edit_rec_acct">
                <?php foreach ($rec_acct as $rec_types) { ?>
                  <option value="<?php echo $rec_types->account_id; ?>" <?php echo($rec_types->account_id == $inv->rec_acct_id ? 'selected' : ''); ?> ><?php echo $rec_types->acct_code." : ".$rec_types->acct_name; ?></option>
                <?php } ?>
              </select>
            </div>
              <label class="col-lg-2 control-label">Invoice Status</label>
            <div class="col-lg-3">
              <select style="width: 159px;" class="chosen-select" name="edit_inv_stat">
                <?php foreach ($inv_status as $lookup_stat) { ?>
                  <option value="<?php echo $lookup_stat->lookup_code; ?>" <?php echo($lookup_stat->lookup_code == $inv->inv_stat ? 'selected' : ''); ?>><?php echo $lookup_stat->meaning; ?></option>
                <?php } ?>
              </select>
            </div>
          </div>
           <div class="form-group">
              <label class="col-lg-9 control-label">Payment Status</label>
            <div class="col-lg-3">
              <p class="form-control-static"><b><?php echo $invoice_pay; ?></b></p>
            </div>
          </div>
          <div class="form-group">
              <label class="col-lg-2 control-label">Description</label>
            <div class="col-lg-5">
              <input type="text" name="edit_inv_desc" class="form-control" value="<?php echo $inv->inv_desc; ?>">
            </div>
              <label class="col-lg-2 control-label">This Invoice Total</label>
            <div class="col-lg-3">
              <input type="text" name="" class="form-control" value="<?php echo $inv->invoice_total; ?>" readonly>
            </div>
          </div>
          <div class="form-group">
              <label class="col-lg-9 control-label">This Invoice Balance</label>
            <div class="col-lg-3">
              <input type="text" name="" class="form-control" value="<?php echo $inv->invoice_balance; ?>" readonly>
            </div>
          </div>
          
        </div>
        <?php } ?>
    </form>
<!--  -->
    <form id="invoice_inline_edit_form">
      <input type="hidden" name="inline_invoice_id" id="e_inline_inv_id">
      <table id="" class="table table-bordered" style="width:99%;">
        <thead>
          <tr>
            <th style="width:50px;">Line</th>
            <th style="">Item #</th>
            <th style="">Description</th>
            <th style="width:90px;">Quantity</th>
            <th style="width:150px;">Unit of Measure</th>
            <th style="width:50px;">Unit Price</th>
            <th style="width:50px;">TAX</th>
            <th style="width:70px;">Amount</th>
          </tr>   
          
        </thead>
      </form>
        <tbody id="edit_inv_line" class="animate fadeIn">
        </tbody>
    </table> 

</div>
<script>
$(document).ready(function(){ 
$('#edit_invoice_form').data('serialize',$('#edit_invoice_form').serialize());
$(".chosen-select").chosen({width:"100%;"}); 
        var i_id = $('#e_invoice_id').val();
        $.ajax({
              type: "POST",
              url: "<?php echo base_url($this->session->userdata('forajax').'/view_invoiceLine'); ?>"+"/"+i_id,
              dataType: 'json',
          success: function(data){
                $('#edit_inv_line').empty();
                var trHTML = '';
                $.each(data, function(i, item) {
                  trHTML += '<tr><td>' + data[i].line_num + '</td><td text-align:right;>' + data[i].item_num + '</td><td text-align:right;>' + data[i].description + '</td><td text-align:right;>' + data[i].usage_qty + '</td><td text-align:right;>' + data[i].uom_name + '</td><td text-align:right;>' + data[i].unit_selling_price + '</td><td text-align:right;>' + data[i].tax_amt + '</td><td text-align:right;>' + data[i].line_amt + '</td><td></td></tr>';
                });
                $('#edit_inv_line').append(trHTML);
              }      
        });

     $('#edit_invoice_form').validate({
        rules:{
          edit_rec_acct   : {
            required : true
          }
        },submitHandler: function(form){
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax').'/update_invoice'); ?>",
                        data: $('#edit_invoice_form').serialize(),
                        dataType: 'json',
                    success: function(data){
                        if(data[0].error == 1){
                            notifymsg("danger",data[0].msg);
                        }else{
                            notifymsg("success",". . Invoice Updated succesfully");
                            close_edit(); active_search();
                        }
                      }
                    });
                 }
      });

      function alert_cancel(){
            if($('#edit_invoice_form').serialize()!=$('#edit_invoice_form').data('serialize')){
                swal({
                    title: "Are you sure?",
                    text: "Your work will not be save",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "Leave",
                    cancelButtonText: "Stay",
                    closeOnConfirm: true,
                    closeOnCancel: true },
                function (isConfirm) {
                    if (isConfirm) {
                        close_edit(); active_search();
                        notifymsg("hide","hide");
                    } else {
                        swal("Cancelled");
                    }
                });
                }else{
                close_edit(); active_search();
                notifymsg("hide","hide");
            }
        } 
        $('#cancel_edit').click(function () {
            alert_cancel();
         });
        $('#editX').click(function(){
            alert_cancel();        
       });   

});
</script>